package com.ljm.travels.service.serviceImpl;

import com.ljm.travels.dao.PlaceDAO;
import com.ljm.travels.dao.ProvinceDAO;
import com.ljm.travels.domain.Place;
import com.ljm.travels.domain.Province;
import com.ljm.travels.service.PlaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class PlaceServiceImpl implements PlaceService {

    @Autowired
    private PlaceDAO placeDAO;

    @Autowired
    ProvinceDAO provinceDAO;

    @Override
    public Place finOne(Integer id) {
        return placeDAO.findOne(id);
    }

    @Override
    public List<Place> findByProvinceIdPage(Integer page, Integer rows, Integer provinceId) {
        int start=(page-1)*rows;
        return placeDAO.findByProvinceIdPage(start,rows,provinceId);
    }

    @Override
    public void update(Place place) {
        placeDAO.update(place);
    }

    @Override
    public void delete(Integer id) {
        Place place=placeDAO.findOne(id);
        Province province=provinceDAO.findOne(place.getProvinceId());
        province.setPlaceCounts(province.getPlaceCounts()-1);
        provinceDAO.update(province);
        placeDAO.delete(id);
    }

    @Override
    public void save(Place place) {
        placeDAO.save(place);
        Province province = provinceDAO.findOne(place.getProvinceId());
        province.setPlaceCounts(province.getPlaceCounts()+1);
        provinceDAO.update(province);
    }

    @Override
    public Integer findByProvinceIdCounts(Integer provinceId) {
        return placeDAO.findByProvinceIdCounts(provinceId);
    }
}
